package server

import (
	"short-video/common/models/mysql"
	user "short-video/common/proto"
)

func VideoList(in *user.VideoListReq) (*user.VideoListResp, error) {
	v := &mysql.Video{}
	page := in.Page
	pageSize := in.PageSize
	if page <= 0 {
		page = 1
	}
	if pageSize < 10 {
		pageSize = 10
	}
	offset := (page - 1) * pageSize
	videos, err := v.FindVideos(pageSize, offset)
	if err != nil {
		return nil, err
	}
	var Items []*user.VideoList
	for _, v := range videos {
		Items = append(Items, &user.VideoList{
			VideoName: v.VideoName,
			VideoImg:  v.VideoImg,
		})
	}
	return &user.VideoListResp{List: Items}, nil
}
func UserCenter(in *user.UserCenterReq) (*user.UserCenterResp, error) {
	v := &mysql.Video{}
	if in.UserId == v.UserId {
		videos, err := v.FindVideo(in.UserId)
		if err != nil {
			return nil, err
		}
		var Items []*user.UserCenter
		for _, v := range videos {
			Items = append(Items, &user.UserCenter{
				VideoName: v.VideoName,
				VideoImg:  v.VideoImg,
			})
		}
		return &user.UserCenterResp{List: Items}, nil
	} else {
		status, err := v.FindVideoStatus(in.UserId)
		if err != nil {
			return nil, err
		}
		var Items []*user.UserCenter
		for _, v := range status {
			Items = append(Items, &user.UserCenter{
				VideoName: v.VideoName,
				VideoImg:  v.VideoImg,
			})
		}
		return &user.UserCenterResp{List: Items}, nil
	}
}
